package com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney;

import android.annotation.SuppressLint;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.test.pudamoney.R;
import com.test.pudamoney.activity_fragment_pudamoney.ServiceFeedbackActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseActivity_pudamoney;
import com.test.pudamoney.adapter_pudamoney.CustomerServiceListAdapter_pudamoney;
import com.test.pudamoney.constant_pudamoney.Constant_pudamoney;
import com.test.pudamoney.dialog_pudamoney.CustomerServiceDialog_pudamoney;
import com.test.pudamoney.dialog_pudamoney.LogoutDialog_pudamoney;
import com.test.pudamoney.util_pudamoney.helper_pudamoney.UserInfoHelper_pudamoney;
import com.test.pudamoney.util_pudamoney.http_pudamoney.HttpManager_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseFragment_pudamoney;

public class PersonalCenterFragment_pudamoney extends BaseFragment_pudamoney {

    private View serviceFeedbackLayout;
    private ListView customerServiceLv;
    private TextView customerServiceDescTv;
    private Button logoutBtn;
    private Button refreshStatusBtn;

    private CustomerServiceListAdapter_pudamoney customerServiceListAdapter;
    private BaseActivity_pudamoney activity;

    private int listHeight = 0;

    public PersonalCenterFragment_pudamoney(){}

    public PersonalCenterFragment_pudamoney(BaseActivity_pudamoney activity){
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal_center_pudamoney, container, false);
        init(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init(View view){

        serviceFeedbackLayout = view.findViewById(R.id.fragment_personal_center_layout_service_feedback);
        customerServiceLv = view.findViewById(R.id.fragment_personal_center_lv_customer_service);
        customerServiceDescTv = view.findViewById(R.id.fragment_personal_center_tv_customer_service_desc);
        logoutBtn = view.findViewById(R.id.fragment_personal_center_btn_logout);
        refreshStatusBtn = view.findViewById(R.id.fragment_personal_center_btn_refresh_status);

        view.findViewById(R.id.fragment_personal_center_layout_customer_service).setOnClickListener(v -> new CustomerServiceDialog_pudamoney(requireContext()).show());

        TextView mobileTv = view.findViewById(R.id.fragment_personal_center_tv_mobile);
        mobileTv.setText(Constant_pudamoney.Mobile_Prefix_Nigeria+" "+UserInfoHelper_pudamoney.getInstance().getHideMobile());
        
        serviceFeedbackLayout.setOnClickListener(v -> activity.startActivity_pudamoney(ServiceFeedbackActivity_pudamoney.class));

        refreshStatusBtn.setOnClickListener(v -> HttpManager_pudamoney.getInstance().getAppShowInfo());
        view.findViewById(R.id.fragment_personal_center_btn_logout).setOnClickListener(v -> new LogoutDialog_pudamoney(activity).show());

        customerServiceListAdapter = new CustomerServiceListAdapter_pudamoney(requireContext());
        customerServiceLv.setAdapter(customerServiceListAdapter);

        updateCustomerService();

    }

    public void updateCustomerService(){

        customerServiceListAdapter.initCustomerService();
        customerServiceListAdapter.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = customerServiceLv.getLayoutParams();
        if (listHeight==0) listHeight = layoutParams.height;
        layoutParams.height = customerServiceListAdapter.getCount()*listHeight;
        customerServiceLv.setLayoutParams(layoutParams);
    }

}
